package com.example.jl.bethinner.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.bethinner.demo.InputString;
import com.bethinner.demo.Location;
import com.bethinner.utils.InputStringDaoHelper;
import com.bethinner.utils.LocationHelper;
import com.example.jl.bethinner.R;

import java.util.List;

public class LocationActivity extends AppCompatActivity {

    private ListView listView;
    private LocationHelper locationHelper;
    private LocaAdapter locaAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        locationHelper = LocationHelper.getInstance(this);
        listView = (ListView) findViewById(R.id.list_view);
        locaAdapter = new LocaAdapter(locationHelper.getAllData(),this);
        listView.setAdapter(locaAdapter);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                InputString inputString = InputStringDaoHelper.getInstance(LocationActivity.this).getDataById(1l);
                                if (null == inputString) {
                                    Toast.makeText(LocationActivity.this,  "新建对象" , Toast.LENGTH_SHORT).show();
                                    inputString = new InputString(1l, "test1");
                                    for (Location location : locationHelper.getAllData()) {
                                        location.setInputString(inputString);
                                        locationHelper.updateData(location);
                                    }
                                    InputStringDaoHelper.getInstance(LocationActivity.this).updateData(inputString);
                                    for (Location b :inputString.getLocationList())
                                        Log.d("Location", "Location Id: " + b.getId());
                                } else {
                                    if (null == inputString.getLocationList()) {
                                        Toast.makeText(LocationActivity.this, inputString.getId() + "--没有列表" , Toast.LENGTH_SHORT).show();

                                    }else
                                    Toast.makeText(LocationActivity.this, inputString.getId() + "--" + inputString.getLocationList().size(), Toast.LENGTH_SHORT).show();
                                }

                                for (Location location : locationHelper.getAllData()) {
                                    location.setInputString(inputString);
                                    locationHelper.updateData(location);
                                }
                                inputString.resetLocationList();
                                InputStringDaoHelper.getInstance(LocationActivity.this).updateData(inputString);
                            }
                        }).show();
            }
        });
    }

    class LocaAdapter extends BaseAdapter {

        private List<Location> locations;
        private Context context;

        public LocaAdapter(List<Location> locations, Context context) {
            this.locations = locations;
            this.context = context;
        }

        @Override
        public int getCount() {
            return locations.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder viewHolder = new ViewHolder();
            Location _location =locations.get(position);
            if (null == convertView) {
                convertView = LayoutInflater.from(context).inflate(R.layout.item_list, null);
                viewHolder.textView = (TextView) convertView.findViewById(R.id.test_view);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.textView.setText(String.format("Latitude:%sLongitude:%s", _location.getLatitude(), _location.getLongitude()));
            return convertView;
        }

        private final class ViewHolder {
            private TextView textView;
        }
    }
}
